package top.aicey.ai.sparring.service;

import lombok.extern.slf4j.Slf4j;
import top.aicey.ai.sparring.dto.req.AiAgentStopRequestDto;
import top.aicey.ai.sparring.dto.res.CommonResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class DelayAIAgentInstanceService {

    @Autowired
    private ImsService imsService;

    @Value("${scheduled.fixedDelay.stop-AI:3}")
    private Integer fixedDelay;

    private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);


    /**
     * 3秒后执行停止AI的动作
     * @param aiAgentInstanceId
     */
    public void delayStopAIAgentInstance(String aiAgentInstanceId){
        if (StringUtils.isEmpty(aiAgentInstanceId)){
            log.info("aiAgentInstanceId is null");
            return;
        }

        executorService.schedule(() -> {
            AiAgentStopRequestDto dto = new AiAgentStopRequestDto();
            dto.setAiAgentInstanceId(aiAgentInstanceId);
            //结束会话
            CommonResponse commonResponse = imsService.stopAIAgentInstance(dto);
            log.info("AI instance does not exist!commonResponse:{}",commonResponse);
        }, fixedDelay, TimeUnit.SECONDS);



    }
}
